import { defineConfig } from 'vite'
import legacy from '@vitejs/plugin-legacy'

import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

// svg 插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

// vant 按需加载插件
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from '@vant/auto-import-resolver'

// https://vitejs.dev/config/
export default defineConfig({
  // base: './',
  base: process.env.NODE_ENV === 'production' ? './' : '/',
  publicDir: 'public',
  resolve: {
    alias: [{ find: '@', replacement: resolve(__dirname, 'src') }]
  },
  plugins: [
    vue(),
    legacy({
      targets: ['defaults', 'not IE 11']
    }),

    // vant 的配置
    Components({
      resolvers: [VantResolver()]
    }),
    // svg 的配置
    createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹
      iconDirs: [resolve(process.cwd(), 'src/assets/svg')],
      // 指定symbolId格式
      symbolId: 'icon-[name]'
    })
  ],
  build: {
    target: ['es2015', 'chrome63'] // 默认是modules,百度说是更改这个会去输出兼容浏览器，尝试没啥作用，先配置吧
  },
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true
      }
    }
  },
  server: {
    host: true,
    open: true,
    port: 3000
  }
})
